// pages/spell_order_detail/spell_order_detail.js
var common = require("../../../common/common.js").common;

const app = getApp();

Page({
    /**
   * 页面的初始数据
   */
    data: {
        order_detail: "",
        //详情数据
        trade_no: "",
        //订单号
        time: ""
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        let that = this;
        that.setData({
            trade_no: options.trade_no,
            spellShareImg: options.spellShareImg,
            domain: app.globalData.domain.slice(0, -10)
        });
        that.getBase();
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {},
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {
        let that = this;
        let data = that.data.order_detail;
        return {
            title: data.pro_name,
            imageUrl: that.data.spellShareImg || data.icon,
            path: "/spell/pages/spell_detail/spell_detail?proceed_id=" + data.proceed_id
        };
    },
    //系统设置
    getBase: function() {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            method: "get",
            data: {},
            success: res => {
                wx.setNavigationBarColor({
                    frontColor: "#ffffff",
                    backgroundColor: res.extra.color_main
                });
                that.setData({
                    color_main: res.extra.color_main,
                    color_minor: res.extra.color_minor
                });
                that.orderDetail();
            }
        });
    },
    //获取订单详情
    orderDetail: function() {
        let that = this;
        let trade_no = that.data.trade_no;
        common.request({
            endpoint: "Groupbuy/order/info",
            method: "post",
            data: {
                trade_no: trade_no
            },
            success: res => {
                that.setData({
                    order_detail: res.extra
                });
                that.countdown();
            }
        });
    },
    //拼团中倒计时
    countdown: function() {
        let that = this;
        let times = that.data.order_detail.remaining_time;
        var timer = null;
        timer = setInterval(function() {
            var day = 0, hour = 0, minute = 0, second = 0;
            //时间默认值
                        if (times > 0) {
                hour = Math.floor(times / (60 * 60)) - day * 24;
                minute = Math.floor(times / 60) - day * 24 * 60 - hour * 60;
                second = Math.floor(times) - day * 24 * 60 * 60 - hour * 60 * 60 - minute * 60;
            }
            if (hour <= 9) hour = "0" + hour;
            if (minute <= 9) minute = "0" + minute;
            if (second <= 9) second = "0" + second;
            that.setData({
                time: hour + ":" + minute + ":" + second
            });
            times--;
        }, 1e3);
        if (times <= 0) {
            clearInterval(timer);
        }
    },
    call: function(e) {
        var phone = e.currentTarget.dataset.phone;
        wx.makePhoneCall({
            phoneNumber: phone
        });
    },
    order_copy: function(e) {
        var copy = e.currentTarget.dataset.copy;
        wx.setClipboardData({
            data: copy,
            success: function(res) {
                wx.showToast({
                    title: "复制成功",
                    icon: "succes",
                    duration: 1e3,
                    mask: true
                });
            }
        });
    }
});